package com.wtwd.campus.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wtwd.campus.constant.Constant;
import com.wtwd.campus.constant.SeparatorConstant;
import com.wtwd.campus.dao.WifiDataMapper;
import com.wtwd.campus.entity.LocationInfo;
import com.wtwd.campus.entity.WifiData;
import com.wtwd.campus.model.dto.WifiDTO;
import com.wtwd.campus.service.WifiDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-08-19
 */
@Service
public class WifiDataServiceImpl implements WifiDataService {

    @Resource
    private WifiDataMapper wifiDataMapper;


    @Override
    public LocationInfo getLocationByWifi(List<WifiDTO> wifis) {
        if (wifis == null || wifis.size() == 0) return null;
        List<WifiData> list = new LambdaQueryChainWrapper<>(wifiDataMapper)
                .in(WifiData::getMac, wifis.stream().map(WifiDTO::getMac).collect(Collectors.toList()))
                .list();
        //筛选出过期数据，无效定位过期数据，和普通数据过期数据
        if (list != null && list.size() > 0) {
            list = list.stream().filter(
                    e -> (e.getDataState().equals(Constant.NUM_STR_ZERO) && new Date().getTime() - e.getUpdateTime().getTime() < INVALID_ACCURACY_TIME * 1000 * 60 * 60 * 24)
                            || (e.getDataState().equals(Constant.NUM_STR_ONE) && new Date().getTime() - e.getUpdateTime().getTime() < CACHE_TIME * 1000 * 60 * 60 * 24)
            ).collect(Collectors.toList());
        }
        int dataSize = list.size();
        if (dataSize == 0) return null;
        Map<String, List<WifiData>> locationGroup =
                list.stream().collect(
                        Collectors.groupingBy(e -> Objects.toString(e.getLat(), SeparatorConstant.BLANK)
                        ));
        int maxGroupNum = 0;
        String maxGroupKey = null;
        //选择出经度命中最多的一组
        for (Map.Entry<String, List<WifiData>> data : locationGroup.entrySet()) {
            int size = data.getValue().size();
            String key = data.getKey();
            if (size > maxGroupNum) {
                maxGroupNum = size;
                maxGroupKey = key;
            }
        }
        if (maxGroupKey == null) {
            return null;
        }
        WifiData wifiData = locationGroup.get(maxGroupKey).get(0);
        LocationInfo locationInfo = new LocationInfo();
        locationInfo.setLng(wifiData.getLng());
        locationInfo.setLat(wifiData.getLat());
        locationInfo.setDataState(wifiData.getDataState());
        locationInfo.setLocationTime(wifiData.getUpdateTime());
        locationInfo.setAccuracy(wifiData.getAccuracy());
        locationInfo.setMatchedMacNum(maxGroupNum);
        locationInfo.setAddress(wifiData.getAddress());
        return locationInfo;
    }

    @Override
    public boolean batchInsertOrUpdate(List<WifiData> wifiDatas) {
        return wifiDataMapper.batchInsertOrUpdate(wifiDatas);
    }


    public static void main(String[] args) {
        String s = Objects.toString(123456, " ");
        System.out.println(s);
    }

}


